#include <stdio.h>
#include <string.h>
#include <string>
#include <openssl/hmac.h>
#include <openssl/evp.h>

int main() {
    // The secret key for hashing
    const char* key = "2F6520DC19FD474298B2C1F35F066A80";

    // The data that we're going to hash
    const  char* data = "appKey=791000528&msg={\"title\":\"组合标签\",\"description\":\"组合标签\"}&platform=0&prodMode=true&tags=level,partition&tagsMode=0&timestamp=1612507361&type=0";
    unsigned char result [EVP_MAX_MD_SIZE] = {0};
    unsigned int res_len = 0;

	HMAC(EVP_sha1(), key, strlen(key), (const unsigned char*)data, (unsigned int)strlen(data), result, &res_len);

    // Be careful of the length of string with the choosen hash engine. SHA1 needed 20 characters.
    /*// Change the length accordingly with your choosen hash engine.*/
    /*unsigned char* result;*/
    /*unsigned int len = 20;*/

    /*result = (unsigned char*)malloc(sizeof(char) * len);*/

    /*[>HMAC_CTX ctx;<]*/
    /*[>HMAC_CTX_init(&ctx);<]*/

    /*[>// Using sha1 hash engine here.<]*/
    /*[>// You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc<]*/
    /*[>HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha1(), NULL);<]*/
    /*[>HMAC_Update(&ctx, (unsigned char*)&data, strlen(data));<]*/
    /*[>HMAC_Final(&ctx, result, &len);<]*/
    /*[>HMAC_CTX_cleanup(&ctx);<]*/

    printf("HMAC digest: ");

    char tmp[10] = {0};
    std::string hex;
    for (int i = 0; i < res_len; ++i)
    {
        memset(tmp, 0, 10);
        printf("%02x", (unsigned int)result[i]);
        snprintf(tmp, 3, "%02x", (unsigned int)result[i]);
        printf("----%d\n", strlen(tmp));
        hex.append(tmp, 2);
    }

    printf("\n");
    printf("%s\n", hex.c_str());


    /*free(result);*/

    return 0;
}
